﻿using Infrastructure.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;
using ZR.ServiceCore.Services;

namespace ZR.Service.Service.ProductionControl
{
    [AppService(ServiceType = typeof(IWorkstationConfigService), ServiceLifetime = LifeTime.Transient)]
    public class WorkstationConfigService : BaseService<WorkstationConfig>, IWorkstationConfigService
    {
        /// <summary>
        /// 查询工位配置表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<WorkstationConfigDto> GetList(WorkstationConfigQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<WorkstationConfig, WorkstationConfigDto>(parm);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public WorkstationConfig GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加工位配置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public WorkstationConfig AddWorkstationConfig(WorkstationConfig model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改工位配置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateWorkstationConfig(WorkstationConfig model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<WorkstationConfig> QueryExp(WorkstationConfigQueryDto parm)
        {
            var predicate = Expressionable.Create<WorkstationConfig>();
            predicate.AndIF(!string.IsNullOrWhiteSpace(parm.HmiCode), ad => ad.HmiCode.Contains(parm.HmiCode));
            predicate.AndIF(!string.IsNullOrWhiteSpace(parm.ProductionLineCode), ad => ad.ProductionLineCode.Contains(parm.ProductionLineCode));
            predicate.AndIF(!string.IsNullOrWhiteSpace(parm.WorkstationCode), ad => ad.WorkstationCode.Contains(parm.WorkstationCode));

            return predicate;
        }

        public List<WorkstationConfig> QueryByHmiCode(string code)
        {
            return Queryable().Where(w => w.HmiCode.Contains(code)).ToList();
        }

        public WorkstationConfig QueryInfo(string line, string station)
        {
            return QueryByClause(qbc => qbc.ProductionLineCode.Equals(line) && qbc.WorkstationCode.Equals(station));
        }
    }
}